package com.lefevre.cms.web.action.payment;


import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.payment.PaymentLog;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.payment.PaymentService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.user.UserService;
import com.lefevre.cms.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付日志管理
 *
 * @author Lefevre
 */
@Controller
public class PaymentLogAction {

    @Resource
    PaymentService paymentService;
    @Resource
    UserService userService;
    @Resource
    SettingService settingService;

    @ResponseBody
    @RequestMapping("/control/paymentLog/list")
    public String execute(ModelMap model, PageForm pageForm, String userName,
                          HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();

        if (userName != null && !"".equals(userName.trim())) {
            //调用分页算法代码
            PageView<PaymentLog> pageView = new PageView<PaymentLog>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10);
            //当前页
            int firstIndex = (pageForm.getPage() - 1) * pageView.getMaxresult();


            User user = userService.findUserByUserName(userName);
            if (user != null) {
                QueryResult<PaymentLog> qr = paymentService.findPaymentLogPage(user.getId(), user.getUserName(), firstIndex, pageView.getMaxresult());

                //将查询结果集传给分页List
                pageView.setQueryResult(qr);

                returnValue.put("currentUser", user);
                returnValue.put("pageView", pageView);
            }
        } else {
            error.put("userName", "用户名称不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }

}

